package com.example.pixelyear.ui.act

import android.os.Bundle
import androidx.recyclerview.widget.ItemTouchHelper
import com.example.pixelyear.Interface.OnChooseColorClick
import com.example.pixelyear.Interface.Touch
import com.example.pixelyear.R
import com.example.pixelyear.adapter.BaseAdapter
import com.example.pixelyear.adapter.EditColorAdapter
import com.example.pixelyear.bean.ColorBean
import com.example.pixelyear.bean.EventTags
import com.example.pixelyear.bean.MessageEvent
import com.example.pixelyear.sql.ColorSqlUtils
import com.example.pixelyear.ui.base.BaseActivity
import com.example.pixelyear.utils.*
import com.example.pixelyear.utils.dialog.BaseChooseColorDialog
import com.example.pixelyear.utils.dialog.ChooseColorDialog
import com.example.pixelyear.utils.dialog.ChooseColorForEggDialog
import com.example.pixelyear.utils.extension.setOnDoubleClickListener
import com.example.pixelyear.utils.extension.verticalManager
import com.socks.library.KLog
import kotlinx.android.synthetic.main.activity_edit_color.*
import org.greenrobot.eventbus.EventBus

class EditColorActivity : BaseActivity() {

    private val mAdapter by lazy {
        EditColorAdapter(mActivity)
    }

    private var lastChoose = -1

    private val colorSql by lazy {
        ColorSqlUtils(mActivity)
    }

    private val colorDialog: BaseChooseColorDialog by lazy {
        if (YxsSpUtils.getBoolean(Constant.OPEN_SENIOR_EDIT_COLOR, false)
            && YxsSpUtils.getBoolean(Constant.OPEN_SENIOR_COLOR, true)
        )
            ChooseColorForEggDialog(mActivity)
        else ChooseColorDialog(mActivity)
    }

    override fun getLayoutResID(): Int {
        return R.layout.activity_edit_color
    }

    override fun initView(bundle: Bundle?) {

        EditColor_Recycler.verticalManager()
        EditColor_Recycler.adapter = mAdapter
        mAdapter.updateList(colorSql.getData())
        ItemTouchHelper(Touch(mAdapter)).attachToRecyclerView(EditColor_Recycler)

        if (mAdapter.size() >= 2) {
            mAdapter.canDrag = true
            mAdapter.canSwipe = true
        }
    }

    override fun initData() {
        DefaultHandler.getInstance().postDelayed({
            if (YxsSpUtils.getBoolean(Constant.FIRST_EDIT_COLOR, true)) {
                showTipsDialog()
                YxsSpUtils.putBoolean(Constant.FIRST_EDIT_COLOR, false)
            }
        }, 500)

    }

    override fun initListener() {
        mAdapter.setOnItemClickListener(object : BaseAdapter.OnItemClick<ColorBean> {
            override fun click(bean: ColorBean, position: Int) {
                KLog.e("yxs", "点击事件")
                colorDialog.show(bean.color, bean.text)
                lastChoose = position
            }

            override fun third(bean: ColorBean, position: Int) {
                if (position <= 0) {
                    LayoutUtils.goneLayout(
                        activity = mActivity,
                        ids = intArrayOf(R.id.EditColor_Recycler)
                    )
                } else
                    LayoutUtils.visibleLayout(mActivity, intArrayOf(R.id.EditColor_Recycler))
            }
        })

        colorDialog.setOnClick(OnChooseColorClick { color, text ->
            if (lastChoose != -1) {
                mAdapter.updateItem(lastChoose, mAdapter.getItemAtPosition(lastChoose).also {
                    it.color = color
                    it.text = text
                })
            }
            colorDialog.dismiss()
        })

        EditColor_Tips.setOnDoubleClickListener {
            showTipsDialog()
        }

        EditColor_Add.setOnDoubleClickListener {
            mAdapter.add(
                ColorBean(UIUtils.getColor(R.color.red_fa6398), "今天的你，也要元气满满哦～"),
                mAdapter.size()
            )
            EditColor_Recycler.scrollToPosition(mAdapter.size())
            if (mAdapter.size() >= 2) {
                mAdapter.canDrag = true
                mAdapter.canSwipe = true
            }
        }

        EditColor_Save.setOnDoubleClickListener {
            loading.showD()
            colorSql.delete()
            mAdapter.l.forEachIndexed { index, colorBean ->
                colorSql.addColor(-1, colorBean.color, colorBean.text, index)
            }
            loading.dismiss()
            Utils.showToast("更新成功！")
            EventBus.getDefault().post(MessageEvent(EventTags.EDIT_COLOR_UPDATE_MAIN_COLOR_LIST))
            finish()
        }
    }

    private fun showTipsDialog() {
        TipsDialogUtils.showSureCancelDialog(
            mActivity,
            content = "点击修改\n长按拖动排序\n左滑删除（慎用，特别灵敏！！！）\n修改颜色不会刷新之前已使用这个颜色的任务哦\n修改完点击右上角√保存哦～",
            cancelText = ""
        ) {}
    }
}
